package com.ccnf.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.ccnf.core.utils.ResultUtil;
import com.ccnf.core.utils.SystemClock;
import com.ccnf.model.entity.CommentThumbs;
import com.ccnf.model.entity.InvitationComment;
import com.ccnf.model.mapper.CommentThumbsMapper;
import com.ccnf.model.mapper.InvitationCommentMapper;
import com.ccnf.model.pojo.CommentDetail;
import com.ccnf.model.query.CommentQuery;
import com.ccnf.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private InvitationCommentMapper invitationCommentMapper;
    @Autowired
    private CommentThumbsMapper commentThumbsMapper;

    @Override
    public Page<CommentDetail> findComment(CommentQuery commentQuery, Page page) {
        return page.setRecords(invitationCommentMapper.selectComments(commentQuery, page));
    }

    @Override
    @Transactional
    public void saveInvitationComment(InvitationComment invitationComment) {
        Long commentParentId = invitationComment.getCommentPid();// 获取评论父ID
        if (commentParentId != null && commentParentId != 0) {
            ResultUtil.validateSQL(invitationCommentMapper.incrementReplyCount(commentParentId), "新增评论失败");// 父评论的回复数加一
        }
        invitationComment.setCommentTime(SystemClock.second());
        ResultUtil.validateSQL(invitationCommentMapper.insert(invitationComment), "新增评论失败");
    }

    @Override
    public CommentDetail findByCommentId(Long commentId, Long userId) {
        return invitationCommentMapper.selectCommentDetailByCommentId(commentId, userId);
    }

    @Override
    @Transactional
    public void commentThumbs(CommentThumbs commentThumbs) {
        Long commentId = commentThumbs.getCommentId();// 点赞的评论ID
        CommentThumbs select = new CommentThumbs();
        select.setCommentId(commentId);
        select.setUserId(commentThumbs.getUserId());
        CommentThumbs record = commentThumbsMapper.selectOne(select);
        Integer isThumbs = commentThumbs.getIsThumbs();// 获取评论点赞状态
        if (record == null) {
            // 不存在点赞记录
            if (isThumbs == 1) {
                ResultUtil.validateSQL(invitationCommentMapper.incrementThumbsCount(commentId), "点赞不成功");// 评论的点赞数加一
                commentThumbs.setThumbsTime(SystemClock.second());
                ResultUtil.validateSQL(commentThumbsMapper.insert(commentThumbs), "点赞不成功");
            }
        } else if (!isThumbs.equals(record.getIsThumbs())) {
            // 存在点赞记录，且点赞状态不同
            if (isThumbs == 0) {
                // 取消点赞
                ResultUtil.validateSQL(invitationCommentMapper.reduceThumbsCount(commentId), "点赞不成功");// 评论的点赞数减一
            } else {
                // 再次点赞
                commentThumbs.setThumbsTime(SystemClock.second());
                ResultUtil.validateSQL(invitationCommentMapper.incrementThumbsCount(commentId), "点赞不成功");// 评论的点赞数加一
            }
            commentThumbs.setVersion(record.getVersion());
            ResultUtil.validateSQL(commentThumbsMapper.updateThumbsStatusByCommentIdAndUserId(commentThumbs), "点赞不成功");
        }
    }
}